﻿namespace RrslLite.Dsl
{
    public abstract class BatchBuilder : IBatchBuilder
    {

        private Batch _currentBatch;

        public virtual IBatchCallBuilder<TTarget> Call<TTarget>() where TTarget : class
        {
            if (_currentBatch == null)
            {
                _currentBatch = CreateBatch();
            }

            return CreateBatchCallBuilder<TTarget>();
        }

        protected abstract IBatchCallBuilder<TTarget> CreateBatchCallBuilder<TTarget>() where TTarget : class;

        protected abstract Batch CreateBatch();

        public virtual Batch CurrentBatch
        {
            get { return _currentBatch; }
        }

        public virtual void Execute()
        {
            var batch = _currentBatch;
            batch.Execute();
            _currentBatch = null;
        }
    }
}